package com.ktjy.service.impl;

import com.ktjy.entity.Supplier;
import com.ktjy.mapper.SupplierMapper;
import com.ktjy.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("supplierService")
@Transactional
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public List<Supplier> findSupplierList(String supplierName) {

        return supplierMapper.getSupplierList(supplierName);
    }

    @Override
    public int saveBatchSupplier(List<Supplier> supplier) {
        int row = 0;
        for (int i = 0; i < supplier.size(); i++) {
            Supplier s = supplier.get(i);
//            if (i == 1) {
//                //插入到第二天数据时,模拟数据库异常,查看事务是否生效
//                throw new RuntimeException("数据库异常");
//            }
            //计入受影响的总行数
            row += supplierMapper.insertSupplier(s);
        }
        return row;
    }

    @Override
    public int delSupplierById(Long id) {
//        if (true) {
//            //插入到第二天数据时,模拟数据库异常,查看事务是否生效
//            throw new RuntimeException("数据库异常");
//        }
        return supplierMapper.deleteSupplier(id);
    }

    @Override
    public int updateSupplierr(Supplier supplier) {
//        if (true) {
//            //插入到第二天数据时,模拟数据库异常,查看事务是否生效
//            throw new RuntimeException("数据库异常");
//        }
        return supplierMapper.updateSupplier(supplier);
    }


    public void setSupplierMapper(SupplierMapper supplierMapper) {
        this.supplierMapper = supplierMapper;
    }
}
